package ChatDemo.server.Handler;

import ChatDemo.message.ChatRequestMessage;
import ChatDemo.message.ChatResponseMessage;
import ChatDemo.server.session.SessionFactory;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

@ChannelHandler.Sharable
public class ChatRequestMessageHandler extends SimpleChannelInboundHandler<ChatRequestMessage> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ChatRequestMessage msg) throws Exception {
        String to = msg.getTo();
        Channel channel = SessionFactory.getSession().getChannel(to);
        if (channel!=null){
            System.out.println(msg.getContent());
            channel.writeAndFlush(new ChatResponseMessage(true,msg.getContent()));
        }else {
            ctx.writeAndFlush(new ChatResponseMessage(false,"对方不在线！"));
        }
    }
}
